//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ArmijoLineSearch.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/Problem.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Optimization/LineSearch.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (Microsoft::FSharp::Core::FSharpOption<Double>^ eps, Microsoft::FSharp::Core::FSharpOption<Double>^ alpha, Microsoft::FSharp::Core::FSharpOption<Double>^ beta) : CLineSearch(CArmijoLineSearch::typeid)
{
    try
    {
#ifdef HANDLE
        _phArmijoLineSearch = NULL;
#endif
        QuantLib::Real _eps = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (eps) ? (QuantLib::Real)ValueHelper::Convert (eps->Value) : 1e-8); //4
        QuantLib::Real _alpha = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (alpha) ? (QuantLib::Real)ValueHelper::Convert (alpha->Value) : 0.05); //4
        QuantLib::Real _beta = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (beta) ? (QuantLib::Real)ValueHelper::Convert (beta->Value) : 0.65); //4
        _ppArmijoLineSearch = new boost::shared_ptr<QuantLib::ArmijoLineSearch> (new QuantLib::ArmijoLineSearch ( _eps,  _alpha,  _beta ));
        SetLineSearch (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (boost::shared_ptr<QuantLib::ArmijoLineSearch>& childNative, Object^ owner) : CLineSearch(CArmijoLineSearch::typeid)
{
#ifdef HANDLE
	_phArmijoLineSearch = NULL;
#endif
	_ppArmijoLineSearch = &childNative;
    _ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
}
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (QuantLib::ArmijoLineSearch& childNative, Object^ owner) : CLineSearch(CArmijoLineSearch::typeid)
{
#ifdef HANDLE
	_phArmijoLineSearch = NULL;
#endif
	_ppArmijoLineSearch = new boost::shared_ptr<QuantLib::ArmijoLineSearch> (&childNative);
    _ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
    _ArmijoLineSearchOwner = owner;
    _LineSearchOwner = owner;
}

Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (CArmijoLineSearch^ copy) : CLineSearch(CArmijoLineSearch::typeid)
{
#ifdef HANDLE
	_phArmijoLineSearch = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppArmijoLineSearch = new boost::shared_ptr<QuantLib::ArmijoLineSearch> (copy->GetShared());
        _ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
    }
}
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (PLATFORM::Type^ t) : CLineSearch(CArmijoLineSearch::typeid)
{
#ifdef HANDLE
	_phArmijoLineSearch = NULL;
#endif
	if (!t->IsSubclassOf(CArmijoLineSearch::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (QuantLib::Handle<QuantLib::ArmijoLineSearch>& childNative, Object^ owner)  : CLineSearch(CArmijoLineSearch::typeid)
{
	_phArmijoLineSearch = &childNative;
	_ppArmijoLineSearch = &static_cast<boost::shared_ptr<QuantLib::ArmijoLineSearch>>(childNative.currentLink());
    _ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
    _ArmijoLineSearchOwner = owner;
}
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (QuantLib::Handle<QuantLib::ArmijoLineSearch> childNative)  : CLineSearch(CArmijoLineSearch::typeid)
{
	_phArmijoLineSearch = &childNative;
	_ppArmijoLineSearch = &static_cast<boost::shared_ptr<QuantLib::ArmijoLineSearch>>(childNative.currentLink());
    _ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CArmijoLineSearch::CArmijoLineSearch (QuantLib::ArmijoLineSearch childNative)  : CLineSearch(CArmijoLineSearch::typeid)
{
#ifdef HANDLE
	_phArmijoLineSearch = NULL;
#endif
	_ppArmijoLineSearch = new boost::shared_ptr<QuantLib::ArmijoLineSearch> (new QuantLib::ArmijoLineSearch (childNative));
    _ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (boost::dynamic_pointer_cast<QuantLib::LineSearch> (*_ppArmijoLineSearch));
}
#endif

Cephei::QL::Math::Optimization::CArmijoLineSearch::~CArmijoLineSearch ()
{
    if (_ppArmijoLineSearch != NULL)
    {
	    delete _ppArmijoLineSearch;
        _ppArmijoLineSearch = NULL;
    }
}
Cephei::QL::Math::Optimization::CArmijoLineSearch::!CArmijoLineSearch ()
{
    if (_ppArmijoLineSearch != NULL)
    {
	    delete _ppArmijoLineSearch;
    }
}
QuantLib::ArmijoLineSearch& Cephei::QL::Math::Optimization::CArmijoLineSearch::GetReference ()
{
    if (_ppArmijoLineSearch == NULL) throw REFNEW NativeNullException ();
	return **_ppArmijoLineSearch;
}
boost::shared_ptr<QuantLib::ArmijoLineSearch>& Cephei::QL::Math::Optimization::CArmijoLineSearch::GetShared ()
{
    if (_ppArmijoLineSearch == NULL) throw REFNEW NativeNullException ();
	return *_ppArmijoLineSearch;
}
QuantLib::ArmijoLineSearch* Cephei::QL::Math::Optimization::CArmijoLineSearch::GetPointer ()
{
    if (_ppArmijoLineSearch == NULL) throw REFNEW NativeNullException ();
	return &**_ppArmijoLineSearch;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ArmijoLineSearch>& Cephei::QL::Math::Optimization::CArmijoLineSearch::GetHandle ()
{
	if (_phArmijoLineSearch == NULL)
	{
		_phArmijoLineSearch = new Handle<QuantLib::ArmijoLineSearch> (*_ppArmijoLineSearch);
	}
	return *_phArmijoLineSearch;
}
#endif
bool Cephei::QL::Math::Optimization::CArmijoLineSearch::HasNative () 
{
	return (_ppArmijoLineSearch != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::IArmijoLineSearch^ Cephei::QL::Math::Optimization::CArmijoLineSearch_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ eps, Microsoft::FSharp::Core::FSharpOption<Double>^ alpha, Microsoft::FSharp::Core::FSharpOption<Double>^ beta)
{
    return REFNEW CArmijoLineSearch ( eps,  alpha,  beta);
}
